package com.jwthhealth_pub.old.healthyscreening.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.healthyscreening.model.HealthyScreenModel;
import com.jwthhealth_pub.old.healthyscreening.view.HealthyScreeningActivity;
import com.jwthhealth_pub.old.healthyscreening.view.HealthyscreeningAnswerActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/21.
 */
public class HealthyScreeningAdapter extends RecyclerView.Adapter {

    public static final String HEALTHYSCREENANSWER_NO = "0";//否
    public static final String HEALTHYSCREENSANSWER_YES = "1";//是

    private Context mContext;
    private List<Integer> answers;
    private ChildQuestionViewHolder childQuestionViewHolder;
    private HashMap<Integer, Integer> checkedListed;
    private boolean hasclick;

    public List<Integer> getAnswers() {
        return answers;
    }

    public HealthyScreeningAdapter(HealthyScreeningActivity healthyScreeningActivity) {
        this.mContext = healthyScreeningActivity;
        answers = new ArrayList<>(20);
        checkedListed = new HashMap<>();
        for (int i=0; i<20; i++) {
            answers.add(i, -1);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_QUESTION.ordinal()) {
            return new QuestionViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_healthyscreen_question, parent, false));
        } else if (viewType == ITEM_TYPE.ITEM_FOOT.ordinal()) {
            return new FootViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_healthyscreen_foot, parent, false));
        } else {
            return new ChildQuestionViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_healthyscreen_childquestion, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        holder.setIsRecyclable(false);
        QuestionViewHolder questionViewHolder = null;
        FootViewHolder footViewHolder = null;
        childQuestionViewHolder = null;

        if (position == 0 || position == 4 || position == 14) {
            questionViewHolder = (QuestionViewHolder) holder;
        } else if (position == 23){
            footViewHolder = (FootViewHolder) holder;
        } else {

            childQuestionViewHolder = (ChildQuestionViewHolder) holder;

            if (checkedListed.containsKey(position)){
                Integer integer = checkedListed.get(position);
                if (integer == 0) {
                    childQuestionViewHolder.yseBtn.setChecked(true);
                } else {
                    childQuestionViewHolder.noBtn.setChecked(true);
                }
            }

            childQuestionViewHolder.questionRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int i) {

                    int index;
                    index = 0;
                    if (position<4) {
                        index = position - 1;
                    } else if (position >4 && position < 14) {
                        index = position - 2;
                    } else if (position > 14 && position < 23) {
                        index = position - 3;
                    }

                    if (i == R.id.item_rb_yes) {
                        answers.set(index, 1);
                        checkedListed.put(position, 0);
                    } else {
                        answers.set(index, 0);
                        checkedListed.put(position, 1);
                    }
                }
            });
        }

        switch (position) {
            case 0:
                questionViewHolder.questionTitle.setText(mContext.getResources().getString(R.string.healthyscreen_question_one));
                break;
            case 1:
                childQuestionViewHolder.indexTv.setText("1");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_one));
                break;
            case 2:
                childQuestionViewHolder.indexTv.setText("2");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_two));
                break;
            case 3:
                childQuestionViewHolder.indexTv.setText("3");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_thi));
                break;
            case 4:
                questionViewHolder.questionTitle.setText(mContext.getResources().getString(R.string.healthyscreen_question_two));
                break;
            case 5:
                childQuestionViewHolder.indexTv.setText("1");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_for));
                break;
            case 6:
                childQuestionViewHolder.indexTv.setText("2");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_fiv));
                break;
            case 7:
                childQuestionViewHolder.indexTv.setText("3");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_six));
                break;
            case 8:
                childQuestionViewHolder.indexTv.setText("4");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_sev));
                break;
            case 9:
                childQuestionViewHolder.indexTv.setText("5");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_eig));
                break;
            case 10:
                childQuestionViewHolder.indexTv.setText("6");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_nin));
                break;
            case 11:
                childQuestionViewHolder.indexTv.setText("7");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_ten));
                break;
            case 12:
                childQuestionViewHolder.indexTv.setText("8");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_ele));
                break;
            case 13:
                childQuestionViewHolder.indexTv.setText("9");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_twi));
                break;
            case 14:
                questionViewHolder.questionTitle.setText(mContext.getResources().getString(R.string.healthyscreen_question_thi));
                break;
            case 15:
                childQuestionViewHolder.indexTv.setText("1");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_thiteen));
                break;
            case 16:
                childQuestionViewHolder.indexTv.setText("2");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_forteen));
                break;
            case 17:
                childQuestionViewHolder.indexTv.setText("3");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_fivteen));
                break;
            case 18:
                childQuestionViewHolder.indexTv.setText("4");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_sixteen));
                break;
            case 19:
                childQuestionViewHolder.indexTv.setText("5");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_sevteen));
                break;
            case 20:
                childQuestionViewHolder.indexTv.setText("6");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_eigteen));
                break;
            case 21:
                childQuestionViewHolder.indexTv.setText("7");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_ninteen));
                break;
            case 22:
                childQuestionViewHolder.indexTv.setText("8");
                childQuestionViewHolder.questionTv.setText(mContext.getResources().getString(R.string.healthyscreen_child_question_twenty));
                break;
            case 23:
                footViewHolder.commitBtn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {

                        for (int i=0; i<20; i++) {
                            if (answers.get(i) == -1) {
                                Toast.makeText(mContext, mContext.getResources().getString(R.string.physicalfiness_alert), Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        String answer = "";
                        for (int i=0; i<20; i++) {
                            answer = answer + answers.get(i) + ",";
                        }
                        answer = answer.substring(0, answer.length()-1);


                        if (!hasclick) {
                            hasclick = true;
                            LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                            Call<HealthyScreenModel> healthyScreenModelCall = null;
                            if (userinfoModel != null) {
                                healthyScreenModelCall = ApiHelper.healthyScreenCommit(userinfoModel.getId(), answer, userinfoModel.getAndroidtoken());
                            } else {
                                healthyScreenModelCall = ApiHelper.healthyScreenCommit(null, answer, null);
                            }

                            Log.d("phesicalFitnessAdapter", "constitutionCompleteModelCall.request().url():" + healthyScreenModelCall.request().url());

                            healthyScreenModelCall.enqueue(new Callback<HealthyScreenModel>() {
                                @Override
                                public void onResponse(Call<HealthyScreenModel> call, Response<HealthyScreenModel> response) {
                                    hasclick = false;
                                    HealthyScreenModel body = response.body();
                                    if(body.getCode().equals("0")){
                                        Intent intent = new Intent(mContext, HealthyscreeningAnswerActivity.class);
                                        intent.putExtra(Constant.HEALTHYSCREENANSWER, body.getData().getResult());
                                        mContext.startActivity(intent);
                                        ((HealthyScreeningActivity) mContext).finish();
                                    } else {
                                        Log.d("HealthyScreeningAdapter", "fail");
                                        ((HealthyScreeningActivity) mContext).runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                ((HealthyScreeningActivity) mContext).proressbar();
                                            }
                                        });
                                    }
                                }

                                @Override
                                public void onFailure(Call<HealthyScreenModel> call, Throwable t) {
                                    Log.d("HealthyScreeningAdapter", "error");
                                }
                            });

                            ((HealthyScreeningActivity) mContext).proressbar();
                        }
                    }
                });
                break;
            default:break;
        }
        
    }

    @Override
    public int getItemCount() {
        return 24;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == 4 || position == 14) {
            return ITEM_TYPE.ITEM_QUESTION.ordinal();
        } else if (position == 23) {
            return ITEM_TYPE.ITEM_FOOT.ordinal();
        } else {
            return ITEM_TYPE.ITEM_CHILD_QUESTION.ordinal();
        }
    }

    private class QuestionViewHolder extends RecyclerView.ViewHolder{

        TextView questionTitle;

        public QuestionViewHolder(View itemView) {
            super(itemView);
            questionTitle = (TextView) itemView.findViewById(R.id.tv_title);
        }
    }

    class ChildQuestionViewHolder extends RecyclerView.ViewHolder{

        TextView questionTv, indexTv;
        RadioGroup questionRg;
        RadioButton yseBtn, noBtn;

        public ChildQuestionViewHolder(View itemView) {
            super(itemView);
            indexTv = (TextView) itemView.findViewById(R.id.tv_index);
            questionTv = (TextView) itemView.findViewById(R.id.item_describe);
            questionRg = (RadioGroup) itemView.findViewById(R.id.psysical_rg);
            yseBtn = (RadioButton) itemView.findViewById(R.id.item_rb_yes);
            noBtn = (RadioButton) itemView.findViewById(R.id.item_rb_no);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{

        Button commitBtn;

        public FootViewHolder(View itemView) {
            super(itemView);
            commitBtn = (Button) itemView.findViewById(R.id.btn_save);
        }
    }

    private enum ITEM_TYPE {
        ITEM_QUESTION, ITEM_CHILD_QUESTION, ITEM_FOOT;
    }
}
